package dao;

import java.util.ArrayList;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

public class ScheduleDAO {
	private EntityManager em;
	
	public ScheduleDAO(EntityManager em) {
		this.em = em;
	}
	
	public void updateEntityManager(EntityManager em){
		this.em = em;
	}
	
	public Schedule searchCode(int oid){
		Query query = em.createNamedQuery("searchScheduleCode");
	    query.setParameter("code", oid);
	    
	    if(query.getResultList().size() > 0){
	    	return (Schedule) query.getSingleResult();
	    }else{
	    	return null;
	    }
	}
	
	public Schedule searchDateTime(Schedule schedule){
		Query query = em.createNamedQuery("searchScheduleDateTime");
		query.setParameter("code", schedule.getOID());
	    query.setParameter("dateTime", schedule.getDhStart());
	    
	    if(query.getResultList().size() > 0){
	    	return (Schedule) query.getSingleResult();
	    }else{
	    	return null;
	    }
	}
	
	public Schedule searchSchedule(Schedule schedule){
		Query query = em.createNamedQuery("searchScheduleCode");
		query.setParameter("code", schedule.getOID());
	    
	    if(query.getResultList().size() > 0){
	    	return (Schedule) query.getSingleResult();
	    }else{
	    	return null;
	    }
	}

	public ArrayList<Schedule> searchSchedule(Date dateS, Date dateE){
		Query query = em.createNamedQuery("searchScheduleDate");
	    query.setParameter("dateS", dateS);
	    query.setParameter("dateE", dateE);
		
		/*String sql = "select * from (select s.OID, s.deService, s.dhEnd, s.dhStart, s.idFit, s.customer_OID " +
				"from Schedule s "	+
				"where s.dhStart between :dateS and :dateE " +

				"union " +

				"Select null, null, null, "+
				"Convert(concat(:dateS,' ',t.nrHours,':', t.nrMinutes), Datetime),  t.idFit, null " +
				"from Timetable t, "+ 
				"Schedule s " + 
				"where t.idFit <> 'S' " +
				"and not (Hour(s.dhStart) = t.nrHours and Minute(s.dhStart) = t.nrMinutes) " +
				"and s.dhStart between :dateS and :dateE) as sel " +
				"Order by dhStart";

		Query query = em.createNativeQuery(sql);
		query.setParameter("dateS", dateS);
	    query.setParameter("dateE", dateE);
	    */
	    ArrayList<Schedule> colSchedule = (ArrayList<Schedule>)query.getResultList();
	    return colSchedule;
	}
	
	public boolean save(Schedule schedule){		
		EntityTransaction trans = em.getTransaction();
		trans.begin();

		try{
			if (em.find(Schedule.class,schedule.getOID()) != null){
				em.merge(schedule);
			}else {
				em.persist(schedule);
			}
			trans.commit();
		}catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
			return false;
		}
		
		em.close();
		DAOFactory.getInstance().updateEntityManager();
		
		return true;
	}	

	public boolean remove(Schedule schedule) {
		EntityTransaction trans = em.getTransaction();
		trans.begin();
		
		try{
			em.remove(schedule);	
			trans.commit();		
		}catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
			return false;
		}
		
		em.close();
		DAOFactory.getInstance().updateEntityManager();
		
		return true;
	}
}








